package org.hot100_3;

import java.util.ArrayList;
import java.util.List;

public class leetcode131 {

    List<List<String>> res = new ArrayList<>();

    List<String> item = new ArrayList<>();

    public List<List<String>> partition(String s) {

        backtracking(s,0);

        return res;
    }

    private void backtracking(String s, int index) {
        if (index == s.length()) {
            res.add(new ArrayList<>(item));
            return;
        }

        for (int i = index; i < s.length(); i++) {
            if (isPartition(s, index, i)) {
                String str = s.substring(index, i + 1);
                item.add(str);
                backtracking(s, i + 1);
                item.remove(item.size()-1);
            }
        }
    }

    private boolean isPartition(String s, int startIndex, int endIndex) {
        if (startIndex == endIndex) {
            return true;
        }

        while (startIndex <= endIndex) {
            if (s.charAt(startIndex) != s.charAt(endIndex)) {
                return false;
            }

            startIndex++;
            endIndex--;
        }

        return true;
    }
}
